package com.atguigu.eduservice.controller;


import com.atguigu.commentutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseCondition;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-11
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //查询课程的基本信息
    @ApiOperation(value = "查询课程的基本信息")
    @GetMapping("findAll")
    public R findAll(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    //条件查询带分页
    @PostMapping("pageCourseComdition/{current}/{limit}")
    @ApiOperation(value = "条件查询带分页")
    public R pageCourseCondition(@ApiParam(value = "当前页") @PathVariable long current,
                                 @ApiParam(value = "每页显示记录数") @PathVariable long limit,
                                 @RequestBody(required = false) CourseCondition courseCondition){

        Page<EduCourse> coursePagepage = new Page<>(current,limit);
        QueryWrapper wrapper = new QueryWrapper();
        String status = courseCondition.getStatus();
        String title = courseCondition.getTitle();
        courseService.page(coursePagepage,wrapper);
        if (!StringUtils.isEmpty(status)){
            wrapper.like("status",status);
        }
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        courseService.page(coursePagepage,wrapper);
        long total = coursePagepage.getTotal();
        List<EduCourse> records = coursePagepage.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //添加课程基本信息
    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.saveCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程ID查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    @ApiOperation(value = "根据课程ID查询课程基本信息")
    public R getCouresInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCouresInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //根据ID修改课程信息
    @PostMapping("updateCourseInfo")
    @ApiOperation(value = "根据ID修改课程信息")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程ID查询课程确认信息
    @ApiOperation(value = "根据课程ID查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("PublishCourse",coursePublishVo);
    }

    //修改课程状态
    @PostMapping("publishCouse/{id}")
    @ApiOperation(value = "修改课程状态")
    public R publishCouse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");//修改课程发布状态
        courseService.updateById(course);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    @ApiOperation(value = "删除课程")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

